package chaojishuzu;

import duixiang.student;

import javax.swing.*;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
 * 主界面
 */
public class StudentUI {
    Scanner sc = new Scanner(System.in);
    SuperArray superArray = new SuperArray();
    /**
     * 菜单
     */
    public void menu() {
        JOptionPane.showMessageDialog(null,"欢迎使用学生管理系统");
        String input;
        while (true) {
            input = JOptionPane.showInputDialog(null,"1-添加学生 2-删除学生 3-修改学生 4-查询学生 5-退出");
            System.out.println("请选择服务类型：1-增加，2-删除，3-修改，4-查询,5-退出");
                if (input.equals("1")) {
                    add();
                } else if (input.equals("2")) {
                    delete();
                } else if (input.equals("3")) {
                    alter();
                } else if (input.equals("4")) {
                    search();
                } else if (input.equals("5")) {
                    JOptionPane.showMessageDialog(null,"再见");
                    System.exit(0);
                }else {
                    JOptionPane.showMessageDialog(null,"输入有误");
                }
            }
        }


    /**
     * 增加
     */
    public void add() {
        JOptionPane.showMessageDialog(null,"这是增加学生信息");
        String name = JOptionPane.showInputDialog(null,"请输入学生姓名：");
        String gender = JOptionPane.showInputDialog(null,"请输入学生性别：");
        int age = 0;
        try {
            age = Integer.parseInt(JOptionPane.showInputDialog(null,"请输入学生年龄："));
        }catch (Exception e){
            JOptionPane.showMessageDialog(null,"年龄输入出错");
            return;
        }
        JOptionPane.showMessageDialog(null,"添加成功请重新选择服务类型");
        Student student = new Student(name,gender,age);
        superArray.add(student);
    }

    /**
     * 删除
     */
    public void delete() {
        JOptionPane.showMessageDialog(null,"这是删除学生信息");
        String name = JOptionPane.showInputDialog(null,"请输入学生姓名：");
        for (int i = 0; i < superArray.size(); i++) {
           // Student student = (Student) superArray.get(i);//强转
            if (superArray.get(i).equals(new Student(name))) {
                superArray.remove(i);
                JOptionPane.showMessageDialog(null,"删除学生信息成功");
                return;
            }
        }
        JOptionPane.showMessageDialog(null,"该学生不存在");
    }

    /**
     * 修改
     */
    public void alter() {
        JOptionPane.showMessageDialog(null,"这是修改学生信息");
        String name = JOptionPane.showInputDialog(null,"请输入要修改的学生姓名：");
        for (int i = 0;i < superArray.size();i++){
            Student student = (Student) superArray.get(i);
            if (student.getName().equals(name)){
                String newName;
                newName = JOptionPane.showInputDialog(null,"请输入学生姓名：");
                student.setName(newName);
                String newGender;
                newGender = JOptionPane.showInputDialog(null,"请输入学生性别：");
                student.setGender(newGender);
                int newAge = 0;
                try {
                    newAge = Integer.parseInt(JOptionPane.showInputDialog(null,"请输入学生年龄："));
                    student.setAge(newAge);
                    JOptionPane.showMessageDialog(null,"修改成功，请重新选择服务类型");
                    return;
                }catch (InputMismatchException e){
                    JOptionPane.showMessageDialog(null,"年龄输入出错");
                    continue;
                }
            }
        }
    }
        /**
         * 查询
         */
       public void search(){
           JOptionPane.showMessageDialog(null,"这是查询学生信息");
           String input = JOptionPane.showInputDialog(null,"请选择 1-全部学生 2-学生姓名");
           if (input.equals("1")){
               for (int i = 0; i < superArray.size(); i++){
                   JOptionPane.showMessageDialog(null,superArray.get(i));
               }
           }else if (input.equals("2")){
               String name = JOptionPane.showInputDialog(null,"请输入学生姓名: ");
           for (int i = 0; i < superArray.size(); i++) {
               //Student student = (Student) superArray.get(i);//强转
               if (superArray.get(i).equals(new Student(name))) {
                   JOptionPane.showMessageDialog(null,superArray.get(i));
                   return;
               }
               else {
                   JOptionPane.showMessageDialog(null,"输入有误");
               }
           }
               JOptionPane.showMessageDialog(null,"未查找到相关学生");
           }

       }
   }

